<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>表格渲染</title>
  </head>
  <body>
    <table border="1" cellspacing="0">
      <thead>
        <tr>
          <th>学号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
        </tr>
      </thead>
      <tbody>

      </tbody>
    </table>
    <script>
      var userList = [
        { id: 1, name: 'Jack', age: 18, gender: '男' },
        { id: 2, name: 'Rose', age: 20, gender: '女' },
        { id: 3, name: 'Tom', age: 22, gender: '男' },
        { id: 4, name: 'Jerry', age: 24, gender: '女' },
      ];


    //1.首先先获取
      var list = document.querySelector('tbody');
      //创建一个空字符来装表格
      var str = '';
      //2.拿出useList的数据并用tr和td做成表格
      userList.forEach(item => {
       str += '<tr>';
        
          // console.log(item);
        // for(var i = 0; i<userList.length;i++){
        
          
        // }
        for( k in item){
          // console.log(item[k]);
          str +='<td>'+item[k]+'</td>'
         
        }
        str += '<tr/>';
      });
      //3.将拿出的数据渲染至页面
      list.innerHTML = str;
    </script>
  </body>
</html>
